package com.erp.activity

import android.app.Activity
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.widget.AdapterView
import com.erp.myAdapter.NotificationAdapter
import com.erp.notifiactionDb.NotificationBean
import com.erp.notifiactionDb.PersonDB
import com.erp.R
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import com.erp.module.crm.xXCrm.ClientKActivity
import com.erp.module.dingDan.prodyctionClient.ProdyctionClientActivity
import kotlinx.android.synthetic.main.activity_notification.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*

/**
 * Created by Administrator on 2016/12/21/021.
 * 作者：刘洋
 */
class NotificationActivity : BaseActivity(), PullToRefreshView.OnFooterRefreshListener {


    lateinit var adapter: NotificationAdapter
    lateinit var mlist: MutableList<NotificationBean>
    lateinit var personDB: PersonDB
    var page = 0
    val pagesize = 20
    private val a = "NotificationActivity"

    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_notification
    }

    override fun initCreate() {
        initView()
        initData()
        initClick()
    }

    private fun initClick() {
        iv_header_left.setOnClickListener{
            val i =Intent()
            setResult(Activity.RESULT_OK, i)
            finish()
        }
        LvNotification.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            var n = NotificationBean()
            n = mlist[position]
            n.setIstrue(true)
            personDB.updata(n)
            if (n.identification == 1) {
                val intent = Intent(this@NotificationActivity, ClientKActivity::class.java)
                intent.putExtra("id", n.oidid)
                intent.putExtra("selsct", 0)
                intent.putExtra("all", "")
                startActivity(intent)
                finish()
            } else if (n.identification == 2) {
                val intent = Intent(this@NotificationActivity, ProdyctionClientActivity::class.java)
                intent.putExtra("id", n.oidid)
                intent.putExtra("selsct", 0)
                intent.putExtra("all", "")
                startActivity(intent)
                finish()
            }
            //                ((Myapp) getApplication()).getApplicationSubject()
            //                        .notifyObserver(Contacts.MAIN, false, null, 100, null, null);
//            initData()
            adapter.notifiAll(mlist)
        }
    }

    private fun initView() {
        tv_header_title.text = "系统通知"
        iv_header_left.visibility = View.VISIBLE
        mlist = ArrayList()
        adapter = NotificationAdapter(this, mlist)
        LvNotification.adapter = adapter
        Pull.setOnFooterRefreshListener(this)
    }

    //    eyuye@qq.com
    private fun initData() {
        personDB = PersonDB.getIntance()
        mlist = personDB.load("id",pagesize,page*pagesize)
        adapter.notifiAll(mlist)
        page++
    }
    override fun onFooterRefresh(view: PullToRefreshView?) {
        Myutil.MyTost(page.toString())
        val  list = personDB.load("id",pagesize,page*pagesize)
        if(list.size>0){
            page++
            for(i in list){
                mlist.add(i)
            }
            adapter.notifiAll(mlist)
        }
        Pull.onFooterRefreshComplete()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        val i =Intent()
        setResult(Activity.RESULT_OK, i)
        return super.onKeyDown(keyCode, event)
    }
}

